import 'dart:developer' as dev;

///日志等级
///* [DEBUD] 调试
///* [INFO] 信息
///* [WARNING] 警告
///* [ERROR] 错误
enum LOGLEVEL { DEBUD, INFO, WARNING, ERROR }

///日志等级枚举扩展
extension LogLevelExtension on LOGLEVEL {
  ///获取tag
  String get tag {
    switch (this) {
      case LOGLEVEL.DEBUD:
        return '[DEBUG]   ';
        break;
      case LOGLEVEL.INFO:
        return '[INFO]    ';
        break;
      case LOGLEVEL.WARNING:
        return '[WARNING] ';
        break;
      case LOGLEVEL.ERROR:
        return '[ERROR]   ';
        break;
    }

    return '[DEFAULT] ';
  }

  ///获取颜色代码
  int get code {
    switch (this) {
      case LOGLEVEL.DEBUD:
        return 5;
        break;
      case LOGLEVEL.INFO:
        return 2;
        break;
      case LOGLEVEL.WARNING:
        return 3;
        break;
      case LOGLEVEL.ERROR:
        return 1;
        break;
    }

    return 0;
  }
}

///日志输出封装
class L {
  ///顶部图案
  static String get _pic {
    return """

         ┌─┐       ┌─┐
      ┌──┘ ┴───────┘ ┴──┐
      │                 │
      │       ───       │
      │  ─┬┘       └┬─  │
      │                 │
      │       ─┴─       │
      │                 │
      └───┐         ┌───┘
          │         │ 神兽保佑
          │         │ 永无BUG
          │         │
          │         └──────────────┐
          │                        │
          │                        ├─┐
          │                        ┌─┘
          │                        │
          └─┐  ┐  ┌───────┬──┐  ┌──┘
            │ ─┤ ─┤       │ ─┤ ─┤
            └──┴──┘       └──┴──┘
  """;
  }

  ///开发者标识
  static String _tag = "DEFAULT";

  ///初始化函数
  static void init({String tag}) {
    _tag = tag ?? _tag;
    i(_pic, showPath: false);
  }

  ///日志着色
  static String _logColor(String info, {int color = 7}) {
    return '\x1B[9${color}m$info\x1B[0m';
  }

  ///打印信息
  static void _log(String content,
      {LOGLEVEL lv = LOGLEVEL.INFO, bool showPath = true}) {
    assert(() {
      final String _time = DateTime.now().toString().split('.')[0];
      final String _currentStack = "package" +
          StackTrace.current.toString().split("\n")[3].split("package")[1];

      dev.log(
        _logColor("[$_tag] ", color: 6) +
            _logColor(lv.tag, color: lv.code) +
            _logColor(content ?? '',
                color: lv == LOGLEVEL.ERROR ? lv.code : 7) +
            (showPath
                ? StackTrace.fromString("\t$_currentStack")
                    .toString()
                    .replaceAll(")", "")
                : ""),
        name: _time,
      );

      return true;
    }());
  }

  ///debug级别日志
  static void d(String msg, {bool showPath = true}) =>
      _log(msg, lv: LOGLEVEL.DEBUD, showPath: showPath);

  ///info级别日志
  static void i(String msg, {bool showPath = true}) =>
      _log(msg, lv: LOGLEVEL.INFO, showPath: showPath);

  ///warning级别日志
  static void w(String msg, {bool showPath = true}) =>
      _log(msg, lv: LOGLEVEL.WARNING, showPath: showPath);

  ///error级别日志
  static void e(String msg) => _log(msg, lv: LOGLEVEL.ERROR);
}
